﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KgUtils.IO
{
    public static partial class RtfFileHelper
    {
        /// <summary>
        /// Removes anny rtf codes between the tags leaving the strue string as return
        /// allso it removes spaces(32) and pipes(|) 
        /// internally it replaces everything it does not know by pipes thats why :-)
        /// </summary>
        /// <param name="rtfString"></param>
        /// <returns></returns>
        public static string CleanRtfString(string rtfString)
        {
            var sb = new StringBuilder(rtfString);
            var cleanSb = new StringBuilder();

            for (var cCounter = 0; cCounter <= sb.Length; cCounter++)
            {
                if (((int)sb[cCounter]) > 32 && sb[cCounter] != '|' && sb[cCounter] != '\\' && sb[cCounter] != '{' && sb[cCounter] != '}')
                    cleanSb.Append(sb[cCounter]);

                if (cCounter + 1 >= sb.Length)
                    break;

                if (sb[cCounter + 1] == '\\' || sb[cCounter + 1] == '{' || sb[cCounter + 1] == '}')
                {
                    for (int dCounter = cCounter + 1; dCounter <= sb.Length - 1; dCounter++)
                    {
                        if (sb[dCounter] == ' ')
                            break;
                        sb[dCounter] = '|';
                    }
                }
            }
            cleanSb.Replace("|", "");
            return cleanSb.ToString();
        }
    }
}
